<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/26
 * Time: 下午3:03
 */
class Order_model extends CI_Model
{
    public function __construct(){
        $this->load->database();
        $this->load->helper("common");
        $this->load->model("user_model");
        $this->load->model("customer_model");
    }


    public function add_order_info($info){
        $this->db->trans_begin();
        $order_info = json_decode($info['order_info'], true);
        if(!empty($order_info) && is_array($order_info))
        {
            $order['order_bn'] = $info['order_bn'];
            $order['shop_customer_id'] = $info['shop_customer_id'];
            $customer_id = $this->user_model->shop_user_id_to_customer_id($info['shop_customer_id']);
            $order['customer_id'] = $customer_id;
            $order['money'] = $info['money'];
            $order['express_fee'] = $info['express_fee'];
            $order['status'] = $info['status'];
            $order['invoice'] = $info['invoice'];
            $order['address'] = $info['address'];
            $order['order_at'] = $info['order_at'];
            $order['detail'] = $info['order_info'];
            $order['created_at'] = date('Y-m-d H:i:s',time());
            $bool = $this->db->insert("app_order", $order);
            $order_id = $this->db->insert_id();
            //分离detail里的数据到表app_order_detail;
            if($order_id){
                foreach ($order_info['products'] as $key => $value) {
                    $order_detail['order_id'] = $order_id;
                    $order_detail['customer_id'] = $customer_id;
                    $order_detail['product_name'] = $value['product_name'];
                    $order_detail['product_id'] = $value['product_id'];
                    $order_detail['sku_info'] = json_encode($value['sku_info']);
                    $order_detail['price'] = $value['final_price'];
                    $order_detail['num'] = $value['count'];
                    $res =$this->db->insert("app_order_detail", $order_detail);
                } 
            }
        }else{
            return false;
        }
        if($bool)
        {
            $this->db->trans_commit();
            return true;
        }else{
            $this->db->trans_rollback();
            return false;
        }
    }

    public function update_order_info($info){
        $this->db->trans_begin();
        $order_info = json_decode($info['order_info'], true);

        if(!empty($order_info) && is_array($order_info))
        {
            $sql = 'select * from app_order WHERE shop_customer_id = '.$info['shop_customer_id'].' and order_bn = '.$info['order_bn'];
            $res = $this->db->query($sql)->row_array();
            if(!$res){
                $this->add_order_info($info);
            }
            $order['money'] = $info['money'];
            $order['shop_order_id'] = $info['shop_order_id'];
            $order['status'] = $info['status'];
            $order['payment_method'] = $info['payment_method'];
            $order['express_fee'] = $info['express_fee'];
            $order['created_at'] = $info['created_at'];
            $order['order_at'] = $info['order_at'];
            $order['detail'] = $info['order_info'];
            if(isset($info['complated_at'])){
                $order['complated_at'] = $info['complated_at'];
            }
            if(isset($info['payment_at'])){
                $order['payment_at'] = $info['payment_at'];
            }
            if(isset($info['shipping_at'])){
                $order['shipping_at'] = $info['shipping_at'];
            }
            //如果detail里的商品信息变动，修改表order_detail
            if(isset($order_info['products'])){
                foreach ($order_info['products'] as $key => $value) {
                    if($value['refundinfo'] == "退款成功"){
                        $row = $this->db->where(array("product_id"=>$value['product_id'],"order_id"=>$res['id']))->from("app_order_detail")->get()->row_array();
                        if($row){
                            if($row['num'] == $value['count']){
                                $this->db->where(array("product_id"=>$value['product_id'],"order_id"=>$res['id']))->delete("app_order_detail");
                            }else{
                                $this->db->where(array("product_id"=>$value['product_id'],"order_id"=>$res['id']));
                                $this->db->set('num',"num -'".$value['count']."'",FALSE);
                                $result = $this->db->update('app_order_detail');
                            }

                            if($res['payment_method'] == 9){
                                //获取admin下的账期类型，1逐月 2逐次
                                $admin_id = $this->customer_model->get_admin_customer_id($res['customer_id']);
                                $admin_info = $this->db->where(array('customer_id'=>$admin_id))->from('app_admin')->get()->row_array();
                                if($admin_info['account_period_type'] == 1){
                                    //更改app_bill里的价格
                                    if($info['status'] == 1){
                                        $this->db->where(array('customer_id'=>$admin_id,'type'=>1));
                                        $this->db->set('money',"money -'".$value['returnprice']."'",FALSE);
                                        if(isset($order_info['returnfright']) && $order_info['returnfright'] != ""){
                                            $this->db->set('money',"money -'".$order_info['returnfright']."'",FALSE);
                                        }
                                        $result = $this->db->order_by('id','desc')->limit(1)->update('app_bill');
                                    }
                                    //更改app_admin账期额度
                                    $this->db->where(array('customer_id'=>$admin_id));
                                    $this->db->set('account_period_quota_used',"account_period_quota_used -'".$value['returnprice']."'",FALSE);
                                    if(isset($order_info['returnfright']) && $order_info['returnfright'] != ""){
                                        $this->db->where(array('order_id'=>$res['id']))->delete('app_order_bill');
                                        $this->db->set('account_period_quota_used',"account_period_quota_used -'".$order_info['returnfright']."'",FALSE);
                                    }
                                    $result = $this->db->update('app_admin');
                                }
                                if($admin_info['account_period_type'] == 2){
                                    $row1 = $this->db->where(array('order_id'=>$res['id']))->from('app_order_bill')->get()->row_array();
                                    $row2 = $this->db->where(array('id'=>$row1['bill_id']))->from('app_bill')->get()->row_array();
                                    //如果app_bill存在。
                                    if($row2){
                                        //更改app_bill里的价格
                                        $this->db->where(array('id'=>$row1['bill_id']));
                                        $this->db->set('money',"money -'".$value['returnprice']."'",FALSE);
                                        if(isset($order_info['returnfright']) && $order_info['returnfright'] != ""){
                                            $this->db->set('money',"money -'".$order_info['returnfright']."'",FALSE);
                                        }
                                        $result = $this->db->update('app_bill');
                                    }
                                    $this->db->where(array('customer_id'=>$admin_id));
                                    $this->db->set('account_period_quota_used',"account_period_quota_used -'".$value['returnprice']."'",FALSE);
                                    if(isset($order_info['returnfright']) && $order_info['returnfright'] != ""){
                                        $this->db->where(array('order_id'=>$res['id']))->delete('app_order_bill');
                                        $this->db->set('account_period_quota_used',"account_period_quota_used -'".$order_info['returnfright']."'",FALSE);
                                    }
                                    $result = $this->db->update('app_admin');
                                }
                            } 
                        }
                        
                    }
                }
            }
            //选中账期支付
            if($info['payment_method'] == 9){
                if($info['status'] == 2){
                    $this->db->insert('app_order_bill',array('order_id'=>$res['id'],'bill_status'=>2));
                }
            }
            if($res['payment_method'] == 9){
                if($info['status'] == 1){
                    //获取admin下的账期类型，1逐月 2逐次
                    $admin_id = $this->customer_model->get_admin_customer_id($res['customer_id']);
                    $admin_info = $this->db->where(array('customer_id'=>$admin_id))->from('app_admin')->get()->row_array();
                    $where = array('order_id'=>$res['id']);
                    $result_array = $this->db->where($where)->from('app_order_detail')->get()->result_array();
                    if($result_array){
                        $money = 0;
                        foreach ($result_array as $key => $value) {
                            $money += $value['price']*$value['num'];
                        }
                        $money = $money + $res['express_fee'];
                        if($admin_info['account_period_type'] == 1){
                            // $date = date("Y-m-d",time());
                            // $this->db->where('bill_date_from <=',$date);
                            // $this->db->where('bill_date_to >=',$date);
                            // $bill = $this->db->from('app_bill_month')->get()->row_array();
                            $bill = $this->db->where(array('customer_id'=>$admin_id))->order_by('id','desc')->limit(1)->from('app_bill')->get()->row_array();
                            if($bill){
                                $this->db->where(array('order_id'=>$res['id']))->update('app_order_bill',array('bill_status'=>1,'bill_id'=>$bill['id'],'bill_date'=>date('Y-m-d',time())));
                                $this->db->where(array('id'=>$bill['id']));
                                $this->db->set('money',"money +'".$money."'",FALSE);
                                $result = $this->db->update('app_bill');
                            }
                        }
                        if($admin_info['account_period_type'] == 2){
                            $bill = array();
                            $bill['customer_id'] = $res['customer_id'];
                            $bill['bill_bn'] = $info['order_bn'];
                            $bill['type'] = 2;
                            $bill['start_repayment_date'] = date("Y-m-d",time());
                            $bill['should_repayment_date'] = date("Y-m-d",time()+($admin_info['account_period_settlement_time']*24*60*60));
                            $bill['money'] = $money;
                            $bill['status'] = 0;
                            $bill['created_at'] = date("Y-m-d H:i:s",time());
                            $this->db->insert('app_bill',$bill);
                            $bill_id = $this->db->insert_id();
                            $this->db->where(array('order_id'=>$res['id']))->update('app_order_bill',array('bill_status'=>1,'bill_id'=>$bill_id,'bill_date'=>date('Y-m-d',time())));
                        }
                    }   
                }
            }

            $bool = $this->db->where(array("shop_customer_id"=>$info['shop_customer_id'],'order_bn'=>$info['order_bn']))->update('app_order',$order);
        }else{
            return false;
        }
        
        if($bool)
        {
            $this->db->trans_commit();
            return true;
        }else{
            $this->db->trans_rollback();
            return false;
        }
    }

}